home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 80 / IOPROG_80.ISO / tips / Java / Comunicazione seriale-parallela / COMUNICAZIONE seriale-parallela.java
Encoding:
Java Source  |  2004-03-29  |  2.9 KB  |  101 lines

  1. import java.io.*;
  2. import javax.comm.*;
  3. import java.util.*;
  4.  
  5. public class Com implements Runnable,SerialPortEventListener{    
  6.     
  7.     //Dichiarazioni variabili
  8.     static CommPortIdentifier portId;
  9.     static Enumeration portList;
  10.       
  11.     SerialPort serialPort;
  12.     Thread readThread;    
  13.     private InputStream serialInput;    
  14.         
  15.     public static void main(String[] arg) throws Exception{    
  16.         
  17.         //Richiede la lista delle porte disponibili
  18.         portList=CommPortIdentifier.getPortIdentifiers();
  19.             
  20.         // Cerca fra le porte disponibili quella richiesta
  21.         //finchΦ c'Φ ancora una porta nella lista
  22.         while (portList.hasMoreElements()){
  23.             
  24.             //Prende un porta dalla lista
  25.             portId=(CommPortIdentifier) portList.nextElement();
  26.             
  27.             // Controlla che la porta presa in considerazione sia
  28.             //una porta seriale ed in particolare la COM1
  29.             // Per le porte parallele utilizzare:
  30.             //CommPortIdentifier.PORT_PARALLEL
  31.             if ((portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
  32.                 && (portId.getName().equals("COM1"))){
  33.                 
  34.                 System.out.println("Porta trovata");
  35.                 
  36.                 new Com();
  37.                 break;            
  38.             }
  39.          }
  40.         }     
  41.    
  42.         
  43.       public Com() throws Exception{
  44.           
  45.           //Apre la porta con un timeout di 2sec (2000 msec)
  46.           serialPort=(SerialPort) portId.open("RS232",2000);
  47.          
  48.          //Associa un evento all'input dalla porta
  49.          serialPort.addEventListener(this);
  50.          serialPort.notifyOnDataAvailable(true);
  51.          
  52.          //Setta i parametri della porta
  53.          serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
  54.         serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);
  55.         
  56.         //Apre un canale di comunicazione
  57.         serialInput=serialPort.getInputStream();
  58.          
  59.          System.out.println("Porta aperta");
  60.          
  61.          //Avvia il thread per l'ascolto
  62.          readThread = new Thread(this);
  63.         readThread.start();
  64.          
  65.      }
  66.      
  67.      //Thread in ascolto sulla tastiera
  68.      public void run(){
  69.          
  70.          try{
  71.              
  72.              BufferedReader tastiera=new BufferedReader(new 
  73.              InputStreamReader(System.in));
  74.          
  75.              //Attesa comando quit
  76.              while (!(tastiera.readLine().equals("quit")));
  77.              
  78.              //Chiusura porta 
  79.              serialPort.close();
  80.          }
  81.          catch (Exception e){
  82.                   System.out.println(e);}
  83.      }
  84.          
  85.          
  86.      public void serialEvent(SerialPortEvent event){
  87.          
  88.          //Verifica la presenza di dati nel buffer
  89.          if (event.getEventType()==SerialPortEvent.DATA_AVAILABLE){
  90.          
  91.              try{
  92.                  //Legge i dati dal buffer (se presenti)
  93.                  while(serialInput.available()>0) 
  94.                        System.out.print((char)serialInput.read());                                 
  95.              } 
  96.              catch (Exception e){
  97.                  System.out.println(e);
  98.               }
  99.          }
  100.      }
  101. }